package hospital.order.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hospital.enums.RefundStatusEnum;
import hospital.model.order.PaymentInfo;
import hospital.model.order.RefundInfo;
import hospital.order.mapper.RefoundInfoMapper;
import hospital.order.service.RefoundInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author
 * @description:
 */
@Service
public class RefoundInfoServiceImpl extends ServiceImpl<RefoundInfoMapper, RefundInfo> implements RefoundInfoService {

    /**
     * @Author yang
     * @Description //TODO 保存退款记录
     * @Param [paymentInfo]
     * @return
     **/
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //根据paymentInfo，查询退款表中是否有记录，没有则添加，有则返回
        QueryWrapper<RefundInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",paymentInfo.getOrderId());
        wrapper.eq("payment_type",paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(wrapper);
        if(null!=refundInfo){
            return refundInfo;
        }

        //没有则添加
        refundInfo=new RefundInfo();
        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
